package com.googlecode.proxjed;

import com.google.common.base.Predicate;
import com.googlecode.recycled.lang.assertions.Assert;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

public class Proxies {

    protected Proxies() {
    }

    public static <T> T proxy(Proxy<T> interceptor) {
        Assert.notNull(interceptor);

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(interceptor.type);
        enhancer.setCallback(interceptor);
        return interceptor.type.cast(enhancer.create());
    }

    public static <T> T nullSafe(T instance, Class<T> type, Predicate<Method> joinPoint) {
        return proxy(new NullSafeProxy<T>(instance, type, joinPoint));
    }

    public static <T> T nullSafe(T instance, Class<T> type) {
        return proxy(new NullSafeProxy<T>(instance, type, null));
    }

}
